import 'package:flutter/material.dart';
import '../services/ScreenAdatper.dart';
import '../widget/jdText.dart';
import '../widget/jdButton.dart';
import '../config/config.dart';
import 'package:dio/dio.dart';
import 'package:fluttertoast/fluttertoast.dart';
class Register1Page extends StatefulWidget {
  Register1Page({Key key}) : super(key: key);

  _Register1PageState createState() => _Register1PageState();
}

class _Register1PageState extends State<Register1Page> {
  String tel;
  sendCode() async{
     RegExp reg = new RegExp(r"^1\d{10}$");
     if(reg.hasMatch(this.tel)){
       var api = "${Config.domain}api/sendCode";

       var reslut = await Dio().post(api,data: {"tel":this.tel});
       
       if(reslut.data['success']){ 
         print(reslut.data);
         Navigator.pushNamed(context, '/register2',arguments: {
           "tel":this.tel,
           "code":reslut.data['code']
         });
       }else{
         Fluttertoast.showToast(
          msg: "${reslut.data['message']}",
          toastLength: Toast.LENGTH_SHORT,
          gravity: ToastGravity.CENTER
        );
       }
     }else{
       Fluttertoast.showToast(
          msg: "手机号格式有误",
          toastLength: Toast.LENGTH_SHORT,
          gravity: ToastGravity.CENTER
        );
     }
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("注册 - 1"),
      ),
      body: ListView(
        padding: EdgeInsets.fromLTRB(20, 0, 20, 0),
        children: <Widget>[ 
          SizedBox(height: 50,),
          JdText(
            text: '请输入手机号',
            onChanged: (value){
              setState(() {
               this.tel = value; 
              });
            },
          ),
          SizedBox(height: 20,),
          JdButtoon(
            color: Colors.red,
            text: '下一步',
            onTap: this.sendCode,
          )
        ],
      ),
    );
  }
}